const MS = require('./ms')
const request = require("./request.js")
const Error = require('./error')
const storage = require("./storage.js")
const {
    isString,
    isNumber,
    isUndefined
} = require('./dataType')
const media = class media {

  constructor() {
  }

  query(options) {
    let params = {
      pageNum: 1,
      pageSize: 10
    }
    if(!isUndefined(options)) {
        if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize
        }
        if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum
        }
        if(!isUndefined(options.title) && isString(options.title)) {
            params.title = options.title
        }
        if(!isUndefined(options.category) && isNumber(options.category)) {
            params.category = options.category
        }
        if(!isUndefined(options.libraryId) && isString(options.libraryId)) {
            params.libraryId = options.libraryId
        }
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId
        }
    }
    return new Promise((resolve, reject) => {
        request(MS._config.parameters.MEDIA.FIND_MEDIA.url ,
            MS._config.parameters.MEDIA.FIND_MEDIA.method, params, {}).then(res => {
                if(res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
        }).catch(err => {
            reject(err)
        })
    })
  }

  get(id) {
    let params = {
      id: id
    }
    return new Promise((resolve, reject) => {
        request(MS._config.parameters.MEDIA.GET_MEDIA.url ,
            MS._config.parameters.MEDIA.GET_MEDIA.method, params, {}).then(res => {
            if(res.success) {
                resolve(res.data)
            } else {
                reject(res.message)
            }
        }).catch(err => {
            reject(err)
        })
    })
  }

  findMediaFile(id) {
    let params = {
      mediaId: id
    }
    return new Promise((resolve, reject) => {
        request(MS._config.parameters.MEDIA.FIND_MEDIA_FILE.url ,
            MS._config.parameters.MEDIA.FIND_MEDIA_FILE.method, params, {}).then(res => {
            if(res.success) {
                resolve(res.data)
            } else {
                reject(res.message)
            }
        }).catch(err => {
            reject(err)
        })
    })
  }
}

module.exports = media
